var db = require('../db/index')
const util = require('util');
// const query = util.promisify(db.query).bind(db)
const query = util.promisify(db.query).bind(db)
exports.getorder = async(req, res) => {
    //接收数据，get 请求数据 如果是post请求 req.body
    // console.log(req.query);
    //获取关键字
    const keyword = req.query.query;
    const page_size = req.query.pagesize; //每页显示的条数
    const page_num = req.query.pagenum; //当前的页码数
    if (page_size == '' || page_num == '') {
        res.ResData("参数错误")
    }
    //业务逻辑
    try {
        // 计算总数
        var sqltotal = "select count(*) as num from sp_order "
        if (keyword) {
            sqltotal += "where order_number like like '%" + keyword + "%'";
        }
        var total = await query(sqltotal);
        // console.log(total[0]);
        total = total[0]['num'];
        if (page_num > Math.ceil(total / page_size)) {
            page_num = Math.ceil(total / page_size)
        }
        var sql = "select * from sp_order "
        // if (keyword) {
        //     sql += "where sp_order.order_number like '%" + keyword + "%'limit ?,?"
        // } else {
        //     sql += "limit ?,?"
        // }
        var params = [(parseInt(page_num) - 1) * parseInt(page_size), parseInt(page_size)]
        var result = await query(sql, params);
        var retorder = []
        for (idx in result) {
            var order = result[idx];
            retorder.push({
                'order_id': order.order_id,
                'user_id': order.user_id,
                'order_number': order.order_number,
                'order_price': order.order_price,
                'order_pay': order.order_pay,
                'is_send': order.is_send,
                'trade_no': order.trade_no,
                'order_fapiao_title': order.order_fapiao_title,
                'order_fapiao_company': order.order_fapiao_company,
                'order_fapiao_content': order.order_fapiao_content,
                'consignee_addr': order.consignee_addr,
                'pay_status': order.pay_status,
                'create_time': order.create_time,
                'update_time': order.update_time,

            })
        }
        const data = {
            "total": total,
            "pagenum": page_num,
            "order": retorder
        }
        res.ResData("获取用户成功", 200, data)



    } catch (err) {
        res.ResData(err)
    }

}